//
//  HTTPHelper+Consume.m
//  SalonManage
//
//  Created by wulongmin on 17/7/16.
//  Copyright © 2017年 lmtt. All rights reserved.
//

#import "HTTPHelper+Consume.h"
#import "ConsumeData.h"

@implementation HTTPHelper (Consume)

- (NSURLSessionDataTask *)getConsumeDataWithBalanceOdd:(NSString *)balance_odd
                                             success:(void(^)(NSArray <ConsumeData *> *consumeList))success {
    
    NSString *url = [API_CONSUME stringByAppendingString:@"list"];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"balance_odd"] = balance_odd;
    
    return [self get:url params:params useToken:YES showText:@"通信中" success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable result, HTTPStatus * _Nonnull status) {
        
        if ([status.code isEqualToString:KF_HTTP_OK]) {
            
            NSMutableArray *consumeList = [ConsumeData mj_objectArrayWithKeyValuesArray:result];
            success(consumeList);
        }else {
            [MBProgressHUD showText:status.msg onView:nil];
            NSLog(@"%@",status);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (NSURLSessionDataTask *)addConsumeWithBalanceOdd:(NSString *)balance_odd
                                          itemCode:(NSString *)item_code
                                          itemName:(NSString *)item_name
                                        consumeNum:(NSInteger)consume_num
                                         unitPrice:(NSString *)unit_price
                                           custNum:(NSString *)cust_num
                                          custKind:(NSInteger)cust_kind
                                           success:(void(^)())success {
    
    NSString *url = [API_CONSUME stringByAppendingString:@"add"];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"balance_odd"] = balance_odd;
    params[@"item_code"] = item_code;
    params[@"item_name"] = item_name;
    params[@"consume_num"] = @(consume_num);
    params[@"unit_price"] = unit_price;
    params[@"cust_num"] = cust_num;
    params[@"cust_kind"] = @(cust_kind);
    params[@"oper_cust_num"] = [UserDefaultsHelper getStringForKey:KF_CUST_NUM];
    params[@"oper_cust_name"] = [UserDefaultsHelper getStringForKey:KF_CUST_NAME];
    
    return [self post:url params:params useToken:YES showText:@"通信中" success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable result, HTTPStatus * _Nonnull status) {
       
        if([status.code isEqualToString:KF_HTTP_OK]){
            
            success();
            
        }else{
            [MBProgressHUD showText:status.msg onView:nil];
            NSLog(@"%@",status);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (NSURLSessionDataTask *)delConsumeWithBalanceOdd:(NSString *)balance_odd
                                          serialno:(NSInteger)serialno
                                           success:(void(^)())success {
    
    NSString *url = [API_CONSUME stringByAppendingString:@"del"];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"balance_odd"] = balance_odd;
    params[@"serialno"] = @(serialno);
    
    return [self post:url params:params useToken:YES showText:@"通信中" success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable result, HTTPStatus * _Nonnull status) {
       
        if ([status.code isEqualToString:KF_HTTP_OK]) {
            
            success();
            
        }else {
            [MBProgressHUD showText:status.msg onView:nil];
            NSLog(@"%@",status);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}



@end
